package com.taplinker.client.im.rpc;

import com.taplinker.client.AppConfigFactory;
import com.taplinker.client.im.SocketTemplateContext;
import com.taplinker.core.rpc.Result;
import com.taplinker.core.rpc.command.AbstractSocketCommand;
import com.taplinker.core.rpc.socket.IMessageRequest;
import com.taplinker.core.rpc.socket.SocketTemplate;
import com.taplinker.core.util.json.JsonUtil;

public class SimpleSocketCommand<T> extends AbstractSocketCommand {
	protected T body;
	private IMMessageRequest b = null;
	
	@Override
	final public IMessageRequest getRequest() {
		if(b == null) {
			b = new IMMessageRequest();
			b.getBuilder().setBody(getBody());
			b.getBuilder().setCommand(getCommand());
			b.getBuilder().setServiceToken(getServiceToken());
		}
		return b;
	}
	
	protected String getCommand() {
		return "";
	}
	
	protected String getBody() {
		if(body == null) {
			return "";
		} else if (body instanceof String) {
			return body.toString();
		} else {
			return JsonUtil.objectToJsonStr(body);
		}
	}
	
	@Override
	final public SocketTemplate getSocketTemplate() {
		return SocketTemplateContext.getInstance().getTemplate();
	}
	
	private String getServiceToken() {
		return AppConfigFactory.getInstance().getUserInfo().getToken();
	}
	
	@Override
	public int getTimeOutSecond() {
		return 60;
	}

	@Override
	public void no(Result result) {
		
	}

	@Override
	public void yes(Result result) {
		
	}

}
